package com.adk.backend.service.impl;

import com.adk.backend.entity.LxOR;
import com.adk.backend.mapper.ORMapper;
import com.adk.backend.service.OrderRemarksService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单状态备注服务实现类
 * 对应PHP的new_add_remarks函数
 */
@Slf4j
@Service
public class OrderRemarksServiceImpl extends ServiceImpl<ORMapper, LxOR> implements OrderRemarksService {
    
    @Autowired
    private ORMapper orMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrderRemarks(Long orderId, Long sId, Integer status, Integer statusTime, 
                                   String rCn, String rEn, String rAr, Integer addTime, Integer addUid) {
        // 参数验证
        if (orderId == null || orderId == 0) {
            log.error("添加订单状态备注失败：订单ID不能为空");
            return false;
        }
        if (status == null || status == 0) {
            log.error("添加订单状态备注失败：订单状态不能为空");
            return false;
        }
        if (rCn == null || rCn.trim().isEmpty()) {
            log.error("添加订单状态备注失败：中文备注不能为空");
            return false;
        }
        
        int currentTime = (int) (System.currentTimeMillis() / 1000);
        
        // 设置默认值
        if (statusTime == null || statusTime == 0) {
            statusTime = currentTime;
        }
        if (addTime == null || addTime == 0) {
            addTime = currentTime;
        }
        
        try {
            // 查询是否已存在相同订单ID、状态和中文备注的记录
            QueryWrapper<LxOR> wrapper = new QueryWrapper<>();
            wrapper.eq("o_id", orderId);
            wrapper.eq("status", status);
            wrapper.eq("r_cn", rCn);
            wrapper.last("limit 1");
            LxOR existing = orMapper.selectOne(wrapper);
            
            if (existing != null) {
                // 如果存在，且status_time不同，则更新status_time
                if (statusTime != null && !statusTime.equals(existing.getStatusTime())) {
                    existing.setStatusTime(statusTime);
                    orMapper.updateById(existing);
                }
                return true;
            } else {
                // 如果不存在，则插入新记录
                LxOR or = new LxOR();
                or.setOId(orderId);
                or.setSId(sId);
                or.setStatus(status);
                or.setStatusTime(statusTime);
                or.setRCn(rCn);
                or.setREn(rEn);
                or.setRAr(rAr);
                or.setAddTime(addTime);
                or.setAddUid(addUid);
                or.setIsShow(1); // 默认显示
                orMapper.insert(or);
                return true;
            }
        } catch (Exception e) {
            log.error("添加订单状态备注失败: orderId={}, status={}, rCn={}", orderId, status, rCn, e);
            return false;
        }
    }
    
    @Override
    public List<LxOR> getRemarksByOrderIdAndStatus(Long orderId, Integer status) {
        if (orderId == null || orderId == 0) {
            return null;
        }
        
        QueryWrapper<LxOR> wrapper = new QueryWrapper<>();
        wrapper.eq("o_id", orderId);
        if (status != null && status != 0) {
            wrapper.eq("status", status);
        }
        wrapper.orderByAsc("status", "id");
        return orMapper.selectList(wrapper);
    }
    
    @Override
    public List<LxOR> getRemarksByOrderId(Long orderId) {
        return getRemarksByOrderIdAndStatus(orderId, null);
    }
}


